
/**   
*    
* 项目名称：a_yongan   
* 类名称：WarehouseController   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月17日 下午8:25:39    
* 修改时间：2018年6月17日 下午8:25:39   
* 修改备注：   
* @version    
*    
*/
package com.yongan.workflow.web;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.user.dto.UserResult;
import com.yongan.util.Execution;
import com.yongan.util.PageBean;
import com.yongan.workflow.entity.FlowInfo;
import com.yongan.workflow.service.FlowInfoService;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/flowinfo")
public class FlowInfoController {

	@Autowired
	private FlowInfoService flowInfoService;
	
	@RequestMapping(value = "/flowinfolist", method = RequestMethod.GET)
	public String flowinfocontent(Model model,HttpServletRequest request) {
		List<FlowInfo> flowinfolist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<FlowInfo> pb = new PageBean<FlowInfo>();
		
		totalRecords = flowInfoService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			flowinfolist = flowInfoService.getFlowInfoList(start, end);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("flowinfolist", flowinfolist);
		return "flowinfolist";
	}
	@RequestMapping(value = "/{flowinfoId}/detail", method = RequestMethod.GET)
	public String flowinfodetail(@PathVariable("flowinfoId") String flowinfoId, Model model,HttpServletRequest request) {
		if (!(flowinfoId.equals("")|flowinfoId==null)) {
			FlowInfo flowInfo = flowInfoService.getFlowInfoById(flowinfoId);
			model.addAttribute("flowInfo", flowInfo);
		}
		return "flowinfodetail";
	}
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> flowinfoupdate(FlowInfo flowInfo, Model model) {
		Execution rs = flowInfoService.executeFlowInfo(flowInfo, flowInfo.getFlowInfoId(), "123321231");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	@RequestMapping(value = "/{flowinfoId}/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> flowinfoDelete(
			@PathVariable("flowinfoId") String flowinfoId, Model model) {
		if (!(flowinfoId.equals("")|flowinfoId==null)) {
			Execution rsdelete = flowInfoService.executeFlowInfo(null, flowinfoId, "1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String flowinfoadd(Model model,HttpServletRequest request) {
//		String number = SerialNumber.Getnum();
//		number = "SCJH"+number;
//		model.addAttribute("number", number);
		//model.addAttribute("salesOrderlist", salesOrderlist);
		return "flowinfoadd";
	}
	@RequestMapping(value = "/adddo", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> flowinfoadddo(FlowInfo flowInfo, Model model) {
		flowInfo.setFlowInfoId(UUID.randomUUID().toString());
		Execution rs = flowInfoService.executeFlowInfo(flowInfo, "", "1233213");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
}
